var app = getApp();
Page(Object.assign({}, {
    data:{
        page : {
            isend : false,
            doing : false,
            doo : 'admin',
            pdata : {
                op : 'userlist',
                page : 1,
                type : 0,
                fxauth:0,
            },
            waitf : 0,
            nodataf : 0,
        },
        datalist : [],
        tab : {
            color : '#449aff',
            list: [{
            id: '0',
                title: '所有用户',
            }, {
            id: '1',
                title: '分销人员',
            }],
            selectedId: '0',
            scroll: false
        },
        actindex : 0,
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
    },
    onLoad:function(options){
        var self = this;
        var cid = options.cid ? options.cid : 0;

        self.data.options = options;
        self.setData({
            datalist : [],
            'page.isend' : false,
            'page.pdata.page' : 1,
            'page.pdata.cardid' : cid,
        })

        app.com.auth(function(user){
            
            app.com.getPage(self,self.data.page,0,function(res){
                var data = res.data.data;
                self.setData({
                   datalist : data.list,
                   params : data.params,
                   inited : true,
                });

            },false,function(res){
                app.com.alert( res.data.message,function(){
                    if( res.data.errno == 2 ){
                        app.globalData.adminurl = app.com.getUrlArgs();
                        wx.navigateTo({
                            url : res.data.data.url
                        });
                    }
                });
            });
            
        },self);

        wx.setNavigationBarTitle({
          title: '用户管理',
        });
        
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: '用户管理',
            path: '',
        }
    },
    onPullDownRefresh: function () {
        this.onLoad();
        // 关闭下拉刷新
        wx.stopPullDownRefresh();
    },
    tab(e) {
        var self = this;
        var fxauth = 0;
        if(e.detail.index > 0) fxauth = 1;
        
        self.setData({
            actindex : e.detail.index,
            'page.pdata.fxauth' : fxauth,
            'page.pdata.page' : 1,
            'page.isend' : false,
            'page.waitf' : 0,
            'page.nodataf' : 0
        });
        app.com.getPage(self,self.data.page,0,function(res){
            self.setData({
                datalist : res.data.data.list,
            });
        });
        self.data.ising = false;

    },
    onReachBottom : function(){
        var self = this;
        app.com.getPage(self,self.data.page,0,function(res){
            self.setData({
                datalist : self.data.datalist.concat(res.data.data.list)
            });
        });
    },
    delfxauth(e){
        var self = this;
        var id =  e.currentTarget.dataset.id;
        var i = e.currentTarget.dataset.i;
        var postdata = {op:'delfxauth',id:id};
        app.com.http('admin','POST',postdata,0,true,'','',function(res){
            if (!res.data.errno && res.data) {
                app.com.toast( res.data.message );
                self.data.datalist.splice(i,1);
                self.setData({
                    datalist : self.data.datalist
                });
            }else{
                app.com.toast( res.data.message );
                if( res.data.errno == 2 ){
                    wx.navigateTo({
                        url : res.data.data.url
                    });
                }
            }
        });

    },
    addfxer(){
        var self = this;
        var postdata = {
            op:'shopqrcode',
            type:'fxer',
        };

        app.com.http('admin','POST',postdata,0,true,'','',function(res){
            if (res.data && !res.data.errno) {
                app.com.alert('发送二维码给好友扫码绑定',function(){
                    wx.previewImage({
                        current: res.data.data.url,
                        urls: [res.data.data.url]
                    })
                });
            }else{
                app.com.toast( res.data.message );
                if( res.data.errno == 2 ){
                    wx.navigateTo({
                        url : res.data.data.url
                    });
                }
            }
        },true);
    },
    input(e){
        var key = e.currentTarget.dataset.key;
        this.setData({
            [`${key}`] : e.detail,
        })
    },
    onPullDownRefresh: function () {
        wx.stopPullDownRefresh();
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    }
}))